package cn.zjj.pet.controller;

import cn.zjj.basic.anno.PreAuthorize;
import cn.zjj.basic.util.JsonResult;
import cn.zjj.basic.util.PageList;
import cn.zjj.pet.domain.Pet;
import cn.zjj.pet.query.PetQuery;
import cn.zjj.pet.service.IPetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
@Api(value = "宠物的API",description="宠物相关的CRUD功能")
public class PetController {

    @Autowired
    private IPetService petService;


    @PostMapping("/onsale")
    @ApiOperation(value = "上架宠物" )
    @PreAuthorize(name = "上架宠物",value = "pet:onsale")
    public JsonResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }
    @PostMapping("/offsale")
    @ApiOperation(value = "下架宠物" )
    @PreAuthorize(name = "下架宠物",value = "pet:offsale")
    public JsonResult offnsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }







    //查询所有
    @GetMapping
    @ApiOperation(value = "查询所有数据" )
    @PreAuthorize(name = "宠物列表",value = "pet:list")
    public List<Pet> selectAll(){

        return petService.selectAll();
    }

    //查询单个
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据" )
    @PreAuthorize(name = "查询宠物",value = "pet:get")
    public Pet selectById(@PathVariable("id") Long id){
        return petService.selectById(id);
    }

    //删除单个
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一个数据" )
    @PreAuthorize(name = "删除宠物",value = "pet:delete")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            petService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //添加或修改
    @PutMapping
    @ApiOperation(value = "添加或修改数据" )
    @PreAuthorize(name = "宠物保存",value = "pet:save")
    public JsonResult updateOrAdd(@RequestBody Pet pet){
        try {
            if(pet.getId()==null){
                petService.insert(pet);
            }else {
                petService.update(pet);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //分页和高级查询
    @PostMapping
    @ApiOperation(value = "分页查询或高级查询" )
    public PageList<Pet> queryPage(@RequestBody PetQuery query){
        return petService.selectQuery(query);
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(name = "宠物批量删除",value = "pet:patchDel")
    public JsonResult delete(@RequestBody List<Long> ids){
        try {
            petService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }
}
